﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ProjectCloth.Entity;
using ProjectCloth.FrmUI.Common;

namespace ProjectCloth.FrmUI
{
    public partial class OnSaleProductFrm : UserControl
    {
        private ClothContext db = new ClothContext();
        public OnSaleProductFrm()
        {
            InitializeComponent();
        }

        private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex == 0 && (dataGridView1.Rows[e.RowIndex].Cells["ProductNo"] == null || dataGridView1.Rows[e.RowIndex].Cells["ProductNo"].Value == null))
            {
                var form = new ProductSelectFrm();
                var result = form.ShowDialog();
                dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = form.productNo;
            }
        }
        //改变键盘键enter效果
        #region
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {

            base.ProcessCmdKey(ref msg, keyData);
            var dgv = dataGridView1;
            bool _disable = false;

            switch (keyData)
            {
                case Keys.Enter:
                    if (dgv.CurrentRow != null && Helper.GetStringFromCell(dgv.CurrentRow.Cells["ProductNo"]).Length > 0)
                    {
                        var index = dgv.CurrentRow.Index;
                        //dgv.CurrentRow.Cells["ProductNo"].Selected = false;
                        //dgv.Rows.Add();
                        //dgv.BackgroundColor = SystemColors.AppWorkspace;
                        //dgv.Rows[index].Selected = false;
                        //dgv.Rows[index].Cells[0].Selected = false;
                        dgv.Rows.Insert(0, new DataGridViewRow());                       
                        dgv.Rows[0].Selected = true ;
                        dgv.Rows[0].Cells[0].Selected = true;
                        //SendKeys.Send("{Tab}");
                    }
                    if (!dgv.Focused)
                    {
                        SendKeys.Send("{Tab}");
                    }                  
                    _disable = true;
                    break;
                case Keys.F12:
                    {
                        //isSave = true;
                        SendKeys.Send("{Tab}");
                        saveBtn_Click(null, null);
                        _disable = true;
                        break;
                    }
                default:
                    break;
            }

            return _disable;
        }
        #endregion
        private void saveBtn_Click(object sender, EventArgs e)
        {
            var dgv = dataGridView1;
            //isSave = true;
            //SendKeys.Send("{Tab}");
            List<OnSaleProduct> list = new List<OnSaleProduct>();
            foreach (DataGridViewRow p in dgv.Rows)
            {
                if (Helper.GetStringFromCell(p.Cells["ProductNo"]).Length > 0)
                {
                    OnSaleProduct os = new OnSaleProduct()
                    {
                        ProductNo = Helper.GetStringFromCell(p.Cells["ProductNo"]),
                        CreateTime = DateTime.Now,
                        OnSaleTime = createTimePick.Value,
                        CreateId = LoginInfo.GetLoginInfo().UserId,
                        Creator = LoginInfo.GetLoginInfo().UserName
                    };
                    list.Add(os);
                }

            }
            db.OnSaleProducts.AddRange(list);
            var itemsResult = db.SaveChanges();
            if (itemsResult > 0)
            {
                MessageBox.Show("保存成功！");
            }
            else
            {
                MessageBox.Show("保存失败！");
            }
            //createTimePick.Value = createTimePick.Value.AddDays(1);
            dgv.Rows.Clear();
            //isSave = false;
        }
    }
}
